package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UserNameExistException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author bihe
 * @email 1067716908@qq.com
 * @date 2023-04-09 15:51:25
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;


    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody SocialUser user) throws Exception {
        MemberEntity login = memberService.login(user);
        if (login != null) {
            return R.ok().setData(login);
        }else {
            return R.error(BizCodeEnume.USERACCO_PASSWORD_EXCEPTION.getCode(), BizCodeEnume.USERACCO_PASSWORD_EXCEPTION.getMsg());
        }
    }








    @RequestMapping("coupons")
    public R coupons(){
        R membercoupons = couponFeignService.membercoupons();
        return membercoupons;
    }


    @PostMapping("/regist")
    public R register(@RequestBody MemberRegistVo vo) {
        try {
            memberService.regist(vo);
        } catch (UserNameExistException e) {
            System.out.println("用户名不唯一");
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            System.out.println("手机号不唯一");
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();

    }


    @PostMapping("/login")
    public R login(@RequestBody MemberEntity vo) {
        MemberEntity login = memberService.login(vo);
        if (login != null) {
            return R.ok().setData(login);
        }else  {
            return R.error(BizCodeEnume.USERACCO_PASSWORD_EXCEPTION.getCode(), BizCodeEnume.USERACCO_PASSWORD_EXCEPTION.getMsg());
        }
//        else {
//            return R.error(BizCodeEnume.PASSWORD_EXCEPTION.getCode(), BizCodeEnume.PASSWORD_EXCEPTION.getMsg());
//        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
